package com.emex.app.web.service;

import com.emex.app.shared.data.Profit;
import com.emex.app.shared.data.ProfitExample;
import com.emex.app.shared.persistance.ProfitMapper;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Sergey Karavaev
 */
@Service
@Transactional
public class ProfitService {

    @Autowired
    private ProfitMapper profitMapper;

    public int deleteProfitByPrimaryKey(Integer id) {
        return profitMapper.deleteByPrimaryKey(id);
    }

    public Profit insertProfit(Profit record) {
        record.setDate(correctDate(record.getDate()));
        profitMapper.insert(record);
        return record;
    }

    public List<Profit> selectProfitByExample(ProfitExample profitExample) {
        List<Profit> list = profitMapper.selectByExample(profitExample);
        correctDate(list);
        return list;
    }

    public Profit selectProfitByPrimaryKey(Integer id) {
        Profit profit = profitMapper.selectByPrimaryKey(id);
        profit.setDate(correctDate(profit.getDate()));
        return profit;
    }

    public Profit updateProfitByPrimaryKey(Profit record) {
        record.setDate(correctDate(record.getDate()));
        profitMapper.updateByPrimaryKey(record);
        return record;
    }

    public List<Profit> getAllProfitsByDate(Date date) {
        ProfitExample profitExample = new ProfitExample();
        profitExample.createCriteria().andDateEqualTo(correctDate(date));
        return selectProfitByExample(profitExample);
    }

    public List<Profit> getAllProfitsByDateEqualOrGraterThan(Date date) {
        ProfitExample profitExample = new ProfitExample();
        profitExample.createCriteria().andDateGreaterThanOrEqualTo(correctDate(date));
        return selectProfitByExample(profitExample);
    }

    private void correctDate(List<Profit> list) {
        if (null != list && !list.isEmpty()) {
            for (Profit record : list) {
                record.setDate(correctDate(record.getDate()));
            }
        }
    }

    private Date correctDate(Date op) {
        if (null != op) {
            final long hours12 = 4L * 60L * 60L * 1000L;
            Date date = new Date(op.getTime() + hours12);
            return date;
        }
        return null;
    }

    public List<Profit> getAllProfitsByDateAndProfitType(Date startDate, Date endDate, Integer profitTypeId) {
        ProfitExample profitExample = new ProfitExample();
        if (null != profitTypeId) {
            profitExample.createCriteria().andDateBetween(correctDate(startDate), correctDate(endDate)).
                    andProfitTypeEqualTo(profitTypeId);
        } else {
            profitExample.createCriteria().andDateBetween(correctDate(startDate), correctDate(endDate));
        }

        return selectProfitByExample(profitExample);
    }
}
